package com.example.demo.sevice.imp;

import com.example.demo.controller.data.ReturnData;
import com.example.demo.dao.CommentDao;
import com.example.demo.dao.UserDao;
import com.example.demo.entity.CircleLike;
import com.example.demo.entity.Comment;
import com.example.demo.entity.User;
import com.example.demo.sevice.CommentService;
import com.example.demo.util.TokenUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Service
public class CommentServiceImp implements CommentService {
    @Resource
    CommentDao commentDao;
    @Resource
    UserDao userDao;

    /**
     * 给圈子评论的接口
     *
     * @param comment
     * @param token
     * @return
     */
    @Override
    public ReturnData add(Comment comment, String token) {
        //获取当前用户信息
        if (StringUtils.isEmpty(comment.getContent())){
            //if (comment.getContent()==null||comment.getContent().equals("")){
                return ReturnData.error("请输入评论内容");
            }
//    }
        String username = TokenUtil.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);
        //添加评论信息
        comment.setUserId(user.getId());
        comment.setTime(new Timestamp(System.currentTimeMillis()));
        commentDao.save(comment);//保存数据
        return ReturnData.ok("评论成功");
    }

    @Override
    public ReturnData getCommentList(int circleId){
        return ReturnData.ok(commentDao.getComment(circleId));
    }

    @Override
    public ReturnData delete(Comment comment, String token) {
        //获取当前用户信息
        String username = TokenUtil.getUsernameByToken(token);
        User user = userDao.findByUsername(username).get(0);

        comment = commentDao.findById(comment.getId()).get();

        if (comment.getUserId() !=user.getId()){
            return  ReturnData.error("没有权限删除");
        }

        commentDao.delete(comment);//删除数据

        return ReturnData.ok("删除成功");
    }
}
